package com.xoa.service.event.impl;

import com.xoa.dao.event.TimelineEventMapper;
import com.xoa.model.event.TimeLine;
import com.xoa.model.event.TimelineEvent;
import com.xoa.service.event.TimeLineEventService;
import com.xoa.util.ToJson;
import com.xoa.util.common.L;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * Created by 张丽军 on 2017/8/30.
 */
@Service
public class TimeLineEventServiceImpl implements TimeLineEventService{
    @Resource
    TimelineEventMapper timelineEventMapper;

    @Override
    public ToJson<TimelineEvent> getEventList(HttpServletRequest request, TimelineEvent timelineEvent,TimeLine timeLine) {
        ToJson<TimelineEvent> toJson=new ToJson<>(1,"error");
        try{
            timeLine.getUuid();
            List<TimelineEvent> list=timelineEventMapper.getEventList(timelineEvent);
            if(list!=null){
                toJson.setObj(list);
                toJson.setMsg("ok");
            }else {
                toJson.setMsg("列表为空");
            }
            toJson.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
            L.e("TimeLineEventServiceImpl getEventList:"+e);
        }
        return toJson;
    }

    @Override
    public ToJson<TimelineEvent> getEventDetailByUuid(HttpServletRequest request, String uuid) {
        ToJson<TimelineEvent> toJson=new ToJson<>(1,"error");
        try{
            TimelineEvent timelineEvent=timelineEventMapper.getEventDetailByUuid(uuid);
            if(timelineEvent!=null){
                toJson.setObject(timelineEvent);
                toJson.setMsg("ok");
            }else {
                toJson.setMsg("数据为空");
            }
            toJson.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
            L.e("TimeLineEventServiceImpl getDetailByUuid:"+e);
        }
        return toJson;
    }

    @Override
    public ToJson<TimelineEvent> updateInfor(HttpServletRequest request, TimelineEvent timelineEvent) {
        ToJson<TimelineEvent> toJson=new ToJson<>(1,"error");
        try{
            int a=timelineEventMapper.updateInfor(timelineEvent);
            if(a>0){
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }else {
                toJson.setMsg("更新失败");
                toJson.setFlag(1);
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
            L.e("TimeLineEventServiceImpl updateInfor:"+e);
        }
        return toJson;
    }


    @Override
    public ToJson<TimelineEvent> addEventInfor(HttpServletRequest request, TimelineEvent timelineEvent, TimeLine timeLine) {
        ToJson<TimelineEvent> toJson=new ToJson<>(1,"error");
        try{
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            timelineEvent.setUuid(uuid);
            timelineEvent.setTimelineUuid(timeLine.getUuid());
            int c=timelineEventMapper.addEventInfor(timelineEvent);
            if(c>0){
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }else {
                toJson.setMsg("新建失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
            L.e("TimeLineEventServiceImpl addEventInfor:"+e);
        }
        return toJson;
    }



    @Override
    public ToJson<TimelineEvent> deleteEventByUuid(HttpServletRequest request, String uuid) {
        ToJson<TimelineEvent> toJson=new ToJson<>(1,"error");
        try{
            int b=timelineEventMapper.deleteEventByUuid(uuid);
            if(b>0){
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }else {
                toJson.setMsg("删除失败");
                toJson.setFlag(1);
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
            L.e("TimeLineEventServiceImpl deleteEventByUuid:"+e);
        }
        return toJson;
    }


}
